﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Portal.Web.Global;
using Portal.Db.Service;
using Portal.Core;
using Portal.Utilities;
using Portal.Web.Framework;
using Portal.AssetTn.Model;
using Portal.Ws.Utilities.Ws;
using Portal.Ws.Utilities;
using System.IO;

namespace Portal.Gui.Web.WebParts.AssetTn
{
    public class AssetSearchResult
    {
        public string HtmlContent;
        public int TotalRecord;
        public int TotalAsset;
    }

    public class AssetBll
    {
        public static string ConnectionString(string SiteId)
        {
            return Environments.GetAppSettings(SiteId, "ConnectionString");
        }

        public static AssetModel CreateModel(string SiteId, string AssetId,bool throwException)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "CreateAssetModel");
            oWsInParam.KeyId = AssetId;
            WsOutParam outParam = UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
            AssetModel oAssetModel = AssetParser.ParseFromXml(outParam.XmlData);
            return oAssetModel; 
        }

        public static AssetTypeCls CreateAssetTypeModel(string SiteId, string AssetTypeId, bool throwException)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "CreateAssetTypeModel");
            oWsInParam.KeyId = AssetTypeId;
            WsOutParam outParam = UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
            AssetTypeCls OAssetType = AssetTypeParser.ParseFromXml(outParam.XmlData);
            return OAssetType;
        }


        public static AssetStatusCls CreateAssetStatusModel(string SiteId, string AssetStatusId, bool throwException)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "CreateAssetStatusModel");
            oWsInParam.KeyId = AssetStatusId;
            WsOutParam outParam = UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
            AssetStatusCls OAssetStatus = AssetStatusParser.ParseFromXml(outParam.XmlData);
            return OAssetStatus;
        }

        public static AssetModel[] Search(
            string SiteId,
            string AssetTypeId, 
            ParamCls[] Params,
            string OrderBy,
            string SortField, 
            string SortType,
            ref DataSet dsResult)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "Search");
            oWsInParam.KeyId = AssetTypeId;
            oWsInParam.OrderBy = OrderBy;
            oWsInParam.ExtraParam1 = SortField;
            oWsInParam.ExtraParam2 = SortType;
            oWsInParam.XmlParams = ParamParser.GetXml(Params);
            WsOutParam outParam = UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
            AssetModel[] AssetModels = AssetParser.ParseMultiRecordFromXml(outParam.XmlData);
            dsResult = new DataSet();
            dsResult.ReadXml(new StringReader(outParam.XmlData));
            //ProcessSearchKeyword(SiteId, Keywords);
            return AssetModels;
        }

        
        public static AssetSearchResult WebRenderResult(
            string SiteId, 
            int    LoadFromBackEnd,
            AssetModel[] AssetModels, 
            int CurrentPageIndex            
            )
        {
            AssetSearchResult oAssetSearchResult = new AssetSearchResult();

            for (int i = 0; i < AssetModels.Length; i++)
            {
                if (!string.IsNullOrEmpty(AssetModels[i].Quantity))
                    oAssetSearchResult.TotalAsset += int.Parse(AssetModels[i].Quantity);
            }

            ReturnPaging oReturnPaging = PagingUtilities.GetPaging(AssetModels.Length, CurrentPageIndex, 20, 8, "SearchNextPage");
            string HtmlBody =                
                oReturnPaging.PagingText+
                   "<div class=\"display:block  ;\">\r\n"+
                   "<table>\r\n";
            for (int iIndex = oReturnPaging.StartIndex; iIndex < oReturnPaging.EndIndex; iIndex++)
            {
                string Url = GetAssetListUrl(SiteId, AssetModels[iIndex].AssetListId, LoadFromBackEnd);
                string Icon = "other.png";
                if (!string.IsNullOrEmpty(AssetModels[iIndex].Icon))
                {
                    Icon = AssetModels[iIndex].Icon;
                }
                HtmlBody +=
                          "<tr>\r\n" +                    
                          "     <td valign=top>\r\n" +
                          "         <table class=tabResultInner>\r\n" +
                          //"             <tr>\r\n" +
                          //"                 <td class=c1>Biểu ghi số</td>\r\n" +
                          //"                 <td class=c2>#" + (iIndex + 1) + "</td>\r\n" +
                          //"             </tr>\r\n" +
                          "             <tr>\r\n" +
                          "                 <td class=c1>Số phân loại:</td>\r\n" +
                          "                 <td class=c2><img src=\"templates/default/images/" + Icon + "\" style=\"float:left;width:32px;margin-right:2px;\"><a href=\"" + Url + "\">" + AssetModels[iIndex].ClassCode + "</a></td>\r\n" +
                          "             </tr>\r\n";
                if (!string.IsNullOrEmpty(AssetModels[iIndex].InventoryCode))
                {
                    HtmlBody +=
                          "             <tr>\r\n" +
                          "                 <td class=c1>Mã số kiểm kê</td>\r\n" +
                          "                 <td class=c2><a href=\"" + Url + "\">" + AssetModels[iIndex].InventoryCode + "</a></td>\r\n" +
                          "             </tr>\r\n";
                }
                //HtmlBody+=
                //          "             <tr>\r\n" +
                //          "                 <td class=c1>Nhan đề hiện vật:</td>\r\n" +
                //          "                 <td class=c2><a href=\"" + Url + "\">" + AssetModels[iIndex].Subject + "</a></td>\r\n" +
                //          "             </tr>\r\n";

                //if (!string.IsNullOrEmpty(AssetModels[iIndex].OtherSubject))
                //{
                //    HtmlBody +=
                //          "             <tr>\r\n" +
                //          "                 <td class=c1>Tên khác:</td>\r\n" +
                //          "                 <td class=c2><a href=\"" + Url + "\">" + AssetModels[iIndex].OtherSubject + "</a></td>\r\n" +
                //          "             </tr>\r\n";
                //}
                //HtmlBody +=
                //          "             <tr>\r\n" +
                //          "                 <td class=c1>Ngày đăng ký:</td>\r\n" +
                //          "                 <td class=c2>" + AssetModels[iIndex].RegDate + "</td>\r\n" +
                //          "             </tr>\r\n";
                //if (!string.IsNullOrEmpty(AssetModels[iIndex].Size))
                //{
                //    HtmlBody +=
                //          "             <tr>\r\n" +
                //          "                 <td class=c1>Kích cỡ:</td>\r\n" +
                //          "                 <td class=c2>" + AssetModels[iIndex].Size + "</td>\r\n" +
                //          "             </tr>\r\n";
                //}
                HtmlBody +=
                          "             <tr>\r\n" +
                          "                 <td class=c1>Số lượng:</td>\r\n" +
                          "                 <td class=c2>" + AssetModels[iIndex].Quantity + "</td>\r\n" +
                          "             </tr>\r\n";
               
                //if (!string.IsNullOrEmpty(AssetModels[iIndex].Material))
                //{
                //    HtmlBody +=
                //          "             <tr>\r\n" +
                //          "                 <td class=c1>Chất liệu:</td>\r\n" +
                //          "                 <td class=c2>" + AssetModels[iIndex].Material + "</td>\r\n" +
                //          "             </tr>\r\n";
                //}
                //if (!string.IsNullOrEmpty(AssetModels[iIndex].MadeBy))
                //{
                //    HtmlBody +=
                //              "             <tr>\r\n" +
                //              "                 <td class=c1>Người tạo:</td>\r\n" +
                //              "                 <td class=c2>" + AssetModels[iIndex].MadeBy + "</td>\r\n" +
                //              "             </tr>\r\n";
                //}
                if (!string.IsNullOrEmpty(AssetModels[iIndex].ContentHistory))
                {
                    HtmlBody +=
                              "             <tr>\r\n" +
                              "                 <td class=c1>Nội dung lịch sử:</td>\r\n" +
                              "                 <td class=c2>" + AssetModels[iIndex].ContentHistory + "</td>\r\n" +
                              "             </tr>\r\n";
                }
                if (!string.IsNullOrEmpty(AssetModels[iIndex].Description))
                {
                    HtmlBody +=
                              "             <tr>\r\n" +
                              "                 <td class=c1>Miêu tả:</td>\r\n" +
                              "                 <td class=c2>" + AssetModels[iIndex].Description + "</td>\r\n" +
                              "             </tr>\r\n";
                }

                if (!string.IsNullOrEmpty(AssetModels[iIndex].NationalName))
                {
                    HtmlBody +=
                              "             <tr>\r\n" +
                              "                 <td class=c1>Dân tộc:</td>\r\n" +
                              "                 <td class=c2>" + AssetModels[iIndex].NationalName + "</td>\r\n" +
                              "             </tr>\r\n";
                }

                //if (!string.IsNullOrEmpty(AssetModels[iIndex].Note))
                //{
                //    HtmlBody +=
                //              "             <tr>\r\n" +
                //              "                 <td class=c1>Ghi chú:</td>\r\n" +
                //              "                 <td class=c2>" + AssetModels[iIndex].Note + "</td>\r\n" +
                //              "             </tr>\r\n";
                //}
                HtmlBody+=
                          //"             <tr>\r\n" +
                          //"                 <td class=c1>Trạng thái:</td>\r\n" +
                          //"                 <td class=c2>" + AssetModels[iIndex].AssetStatusName + "</td>\r\n" +
                          //"             </tr>\r\n" +
                          "         </table>\r\n" +
                          "     </td>\r\n" +
                          "  </tr>\r\n"+
                          " <tr class=trBreakLine>\r\n" +
                          "     <td colspan=2></td>\r\n" +
                          " </tr>\r\n";

                #region backup
                //HtmlBody +=
                //          "<tr>\r\n" +
                //    //"     <td valign=top><center><img src=\"templates/default/images/no-image.png\" class=CssAvatarImage><br>#" + (iIndex + 1) + "</center></td>\r\n" +
                //          "     <td valign=top>\r\n" +
                //          "         <table class=tabResultInner>\r\n" +
                //          "             <tr>\r\n" +
                //          "                 <td class=c1>Biểu ghi số</td>\r\n" +
                //          "                 <td class=c2>#" + (iIndex + 1) + "</td>\r\n" +
                //          "             </tr>\r\n" +
                //          "             <tr>\r\n" +
                //          "                 <td class=c1>Số phân loại:</td>\r\n" +
                //          "                 <td class=c2><img src=\"templates/default/images/" + Icon + "\" style=\"float:left;width:32px;margin-right:2px;\">" + AssetModels[iIndex].ClassCode + "</td>\r\n" +
                //          "             </tr>\r\n";
                //if (!string.IsNullOrEmpty(AssetModels[iIndex].InventoryCode))
                //{
                //    HtmlBody +=
                //          "             <tr>\r\n" +
                //          "                 <td class=c1>Mã số kiểm kê</td>\r\n" +
                //          "                 <td class=c2>" + AssetModels[iIndex].InventoryCode + "</td>\r\n" +
                //          "             </tr>\r\n";
                //}
                //HtmlBody +=
                //          "             <tr>\r\n" +
                //          "                 <td class=c1>Nhan đề hiện vật:</td>\r\n" +
                //          "                 <td class=c2><a href=\"" + Url + "\">" + AssetModels[iIndex].Subject + "</a></td>\r\n" +
                //          "             </tr>\r\n";

                //if (!string.IsNullOrEmpty(AssetModels[iIndex].OtherSubject))
                //{
                //    HtmlBody +=
                //          "             <tr>\r\n" +
                //          "                 <td class=c1>Tên khác:</td>\r\n" +
                //          "                 <td class=c2><a href=\"" + Url + "\">" + AssetModels[iIndex].OtherSubject + "</a></td>\r\n" +
                //          "             </tr>\r\n";
                //}
                //HtmlBody +=
                //          "             <tr>\r\n" +
                //          "                 <td class=c1>Ngày đăng ký:</td>\r\n" +
                //          "                 <td class=c2>" + AssetModels[iIndex].RegDate + "</td>\r\n" +
                //          "             </tr>\r\n";
                //if (!string.IsNullOrEmpty(AssetModels[iIndex].Size))
                //{
                //    HtmlBody +=
                //          "             <tr>\r\n" +
                //          "                 <td class=c1>Kích cỡ:</td>\r\n" +
                //          "                 <td class=c2>" + AssetModels[iIndex].Size + "</td>\r\n" +
                //          "             </tr>\r\n";
                //}
                //HtmlBody +=
                //          "             <tr>\r\n" +
                //          "                 <td class=c1>Số lượng:</td>\r\n" +
                //          "                 <td class=c2>" + AssetModels[iIndex].Quantity + "</td>\r\n" +
                //          "             </tr>\r\n";

                //if (!string.IsNullOrEmpty(AssetModels[iIndex].Material))
                //{
                //    HtmlBody +=
                //          "             <tr>\r\n" +
                //          "                 <td class=c1>Chất liệu:</td>\r\n" +
                //          "                 <td class=c2>" + AssetModels[iIndex].Material + "</td>\r\n" +
                //          "             </tr>\r\n";
                //}
                //if (!string.IsNullOrEmpty(AssetModels[iIndex].MadeBy))
                //{
                //    HtmlBody +=
                //              "             <tr>\r\n" +
                //              "                 <td class=c1>Người tạo:</td>\r\n" +
                //              "                 <td class=c2>" + AssetModels[iIndex].MadeBy + "</td>\r\n" +
                //              "             </tr>\r\n";
                //}
                //if (!string.IsNullOrEmpty(AssetModels[iIndex].ContentHistory))
                //{
                //    HtmlBody +=
                //              "             <tr>\r\n" +
                //              "                 <td class=c1>Nội dung lịch sử:</td>\r\n" +
                //              "                 <td class=c2>" + AssetModels[iIndex].ContentHistory + "</td>\r\n" +
                //              "             </tr>\r\n";
                //}
                //if (!string.IsNullOrEmpty(AssetModels[iIndex].Description))
                //{
                //    HtmlBody +=
                //              "             <tr>\r\n" +
                //              "                 <td class=c1>Miêu tả:</td>\r\n" +
                //              "                 <td class=c2>" + AssetModels[iIndex].Description + "</td>\r\n" +
                //              "             </tr>\r\n";
                //}
                //if (!string.IsNullOrEmpty(AssetModels[iIndex].Note))
                //{
                //    HtmlBody +=
                //              "             <tr>\r\n" +
                //              "                 <td class=c1>Ghi chú:</td>\r\n" +
                //              "                 <td class=c2>" + AssetModels[iIndex].Note + "</td>\r\n" +
                //              "             </tr>\r\n";
                //}
                //HtmlBody +=
                //          "             <tr>\r\n" +
                //          "                 <td class=c1>Trạng thái:</td>\r\n" +
                //          "                 <td class=c2>" + AssetModels[iIndex].AssetStatusName + "</td>\r\n" +
                //          "             </tr>\r\n" +
                //          "         </table>\r\n" +
                //          "     </td>\r\n" +
                //          "  </tr>\r\n" +
                //          " <tr class=trBreakLine>\r\n" +
                //          "     <td colspan=2></td>\r\n" +
                //          " </tr>\r\n";
                #endregion

            }

            HtmlBody += "</table>\r\n" +
                "</div>\r\n";            
            oAssetSearchResult.HtmlContent = HtmlBody;
            oAssetSearchResult.TotalRecord = AssetModels.Length;            

            return oAssetSearchResult;
        }

        public static void ProcessSearchKeyword(string SiteId, Collection<string> Keywords)
        {
            string textKeywords = "";
            for (int iIndex = 0; iIndex < Keywords.Count; iIndex++)
            {
                textKeywords = textKeywords + Keywords[iIndex];
                if (iIndex < Keywords.Count - 1)
                {
                    textKeywords = textKeywords + ";";
                }
            }
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "ProcessSearchKeyword");
            oWsInParam.Keyword = textKeywords;
            UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
        }

        public static AssetImageCls[] ReadingAssetImage(string SiteId,string AssetId)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "ReadingAssetImage");
            oWsInParam.KeyId = AssetId;
            WsOutParam OutParam = UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
            AssetImageCls[] AssetImages = AssetImageParser.ParseMultiRecordFromXml(OutParam.XmlData);
            if (AssetImages == null) AssetImages = new AssetImageCls[0];
            return AssetImages;
        }


        public static AssetListRelationCls[] ReadingAssetListRelation(string SiteId, string AssetId)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "ReadingAssetListRelation");
            oWsInParam.KeyId = AssetId;
            WsOutParam OutParam = UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
            AssetListRelationCls[] AssetListRelations = AssetListRelationParser.ParseMultiRecordFromXml(OutParam.XmlData);
            if (AssetListRelations == null) AssetListRelations = new AssetListRelationCls[0];
            return AssetListRelations;
        }

        public static string GetAssetListUrl(string SiteId, string AssetId,int LoadFromBackEnd)
        {
            string Url = "v-asset.aspx?AssetId=" + AssetId.Substring(0, 8);
            //if (LoadFromBackEnd == 1)
            //{
            //    Url = Screen.BuildUrl(SiteId, "AssetTN.ViewRecord", new ParamCls[]{
            //            new ParamCls("AssetId",AssetId.Substring(0,8))
            //        });
            //}
            return Url;
        }


        public static AssetHistoryCls[] ReadingAssetHistory(string SiteId, string AssetId)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "ReadingAssetListRelation");
            oWsInParam.KeyId = AssetId;
            WsOutParam OutParam = UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
            AssetHistoryCls[] AssetHistories = AssetHistoryParser.ParseMultiRecordFromXml(OutParam.XmlData);
            if (AssetHistories == null) AssetHistories = new AssetHistoryCls[0];
            return AssetHistories;
        }

        public static void SaveAsset(string SiteId,string AssetId, AssetModel oAssetModel)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "SaveAsset");
            oWsInParam.KeyId = AssetId;
            if (!string.IsNullOrEmpty(oAssetModel.MadeDate))
            {
                DateTime MadeDate = FuncUtilities.VNDateToDate(oAssetModel.MadeDate);
                oAssetModel.MadeDate = MadeDate.ToString();
            }
            if (!string.IsNullOrEmpty(oAssetModel.CollectDate))
            {
                DateTime CollectDate = FuncUtilities.VNDateToDate(oAssetModel.CollectDate);
                oAssetModel.CollectDate = CollectDate.ToString();
            }

            if (!string.IsNullOrEmpty(oAssetModel.RegDate))
            {
                DateTime RegDate = FuncUtilities.VNDateToDate(oAssetModel.RegDate);
                oAssetModel.RegDate = RegDate.ToString();
            }
            //doan nay sua lai date
            oWsInParam.XmlData = AssetParser.GetXml(oAssetModel);
            UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
        }


        public static void DeleteAsset(string SiteId, string AssetId)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "DeleteAsset");
            oWsInParam.KeyId = AssetId;
            UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
        }

     

        public static void AddAssetImages(
            string SiteId, 
            string AssetId, 
            string[] Files)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "AddAssetImages");
            oWsInParam.KeyId = AssetId;
            oWsInParam.ExtraParam1 = "";
            for (int iIndex = 0; iIndex < Files.Length; iIndex++)
            {
                oWsInParam.ExtraParam1 = oWsInParam.ExtraParam1 + Files[iIndex] + ";";
            }
            UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
        }

        public static void DeleteAssetImage(string SiteId, string AssetListImageId)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "DeleteAssetImage");
            oWsInParam.KeyId = AssetListImageId;
            UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
        }


        public static AssetTypeCls[] ReadingAssetType(string SiteId)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "ReadingAssetType");
            WsOutParam OutParam = UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
            AssetTypeCls[] AssetTypes = AssetTypeParser.ParseMultiRecordFromXml(OutParam.XmlData);
            if (AssetTypes == null) AssetTypes = new AssetTypeCls[0];
            return AssetTypes;
        }


        public static AssetStatusCls[] ReadingAssetStatus(string SiteId)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "ReadingAssetStatus");
            WsOutParam OutParam = UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
            AssetStatusCls[] AssetStatus = AssetStatusParser.ParseMultiRecordFromXml(OutParam.XmlData);
            if (AssetStatus == null) AssetStatus = new AssetStatusCls[0];
            return AssetStatus;
        }

        public static AssetMaterialCls[] ReadingAssetMaterial(string SiteId)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "ReadingAssetMaterial");
            WsOutParam OutParam = UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
            AssetMaterialCls[] AssetMaterials = AssetMaterialParser.ParseMultiRecordFromXml(OutParam.XmlData);
            if (AssetMaterials == null) AssetMaterials = new AssetMaterialCls[0];
            return AssetMaterials;
        }

        public static AssetNationalCls[] ReadingAssetNational(string SiteId)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "ReadingAssetNational");
            WsOutParam OutParam = UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
            AssetNationalCls[] AssetNationals = AssetNationalParser.ParseMultiRecordFromXml(OutParam.XmlData);
            if (AssetNationals == null) AssetNationals = new AssetNationalCls[0];
            return AssetNationals;
        }

        public static void SaveAssetTypeFieldConfig(string SiteId, string AssetTypeId, AssetViewField[] AssetViewFields)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "SaveAssetTypeFieldConfig");
            string Xml = AssetViewFieldParser.GetXml(AssetViewFields);
                
            oWsInParam.KeyId = AssetTypeId;
            oWsInParam.XmlData = Xml;
            UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
        }


        public static void SaveAssetTypeSearchFieldConfig(string SiteId, string AssetTypeId, AssetViewField[] AssetViewFields)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "SaveAssetTypeSearchFieldConfig");
            string Xml = AssetViewFieldParser.GetXml(AssetViewFields);

            oWsInParam.KeyId = AssetTypeId;
            oWsInParam.XmlData = Xml;
            UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
        }


        public static void SaveAssetTypePrintFieldConfig(string SiteId, string AssetTypeId, AssetViewField[] AssetViewFields)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "SaveAssetTypePrintFieldConfig");
            string Xml = AssetViewFieldParser.GetXml(AssetViewFields);

            oWsInParam.KeyId = AssetTypeId;
            oWsInParam.XmlData = Xml;
            UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
        }

        public static string AddAsset(
            string SiteId,
            AssetModel OAssetModel)
        {
            SiteParam oSiteParam = Environments.CreateSiteParam(SiteId);
            WsInParam oWsInParam = WebServiceUtility.CreateWsInParam(oSiteParam, "Portal.Ws.AssetTn.Bussiness.dll", "AddAsset");
            string Xml = AssetParser.GetXml(OAssetModel);
            oWsInParam.XmlData = Xml;
            WsOutParam OutParam = UtilitiesBll.ProcessCommand(SiteId, oWsInParam);
            return OutParam.RetKeyId;
        }
    }
}
